package client

import (
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"likeadmin/admin/schemas/req"
	"likeadmin/admin/schemas/resp"
	"likeadmin/config"
	"likeadmin/core/request"
	"likeadmin/core/response"
	"likeadmin/model/client"
	"likeadmin/util"
	"strconv"
)

type IClientUserService interface {
	List(c *gin.Context, page request.PageReq, listReq req.ClientUserListReq) (res response.PageResp, e error)
	Detail(id uint) (res resp.ClientUserResp, e error)
	Del(id uint) (e error)
}

// NewClientUserService 初始化
func NewClientUserService(db *gorm.DB) IClientUserService {
	return &clientUserService{db: db}
}

// clientUserService 用户信息服务实现类
type clientUserService struct {
	db *gorm.DB
}

// List 用户信息列表
func (srv clientUserService) List(c *gin.Context, page request.PageReq, listReq req.ClientUserListReq) (res response.PageResp, e error) {
	// 分页信息
	limit := page.PageSize
	offset := page.PageSize * (page.PageNo - 1)
	// 查询
	model := srv.db.Model(&client.ClientUser{})

	adminId := config.AdminConfig.GetAdminId(c)
	if adminId != 1 {
		model = model.Where("creater_id = ?", adminId)
	}

	if listReq.Id != 0 {
		model = model.Where("id = ?", listReq.Id)
	}
	if listReq.Sn != "" {
		model = model.Where("sn = ?", listReq.Sn)
	}
	if listReq.Provider != "" {
		model = model.Where("provider = ?", listReq.Provider)
	}
	if listReq.UserType >= 0 {
		model = model.Where("user_type = ?", listReq.UserType)
	}
	if listReq.IsBlack >= 0 {
		model = model.Where("is_black = ?", listReq.IsBlack)
	}

	if listReq.IsJump >= 0 {
		model = model.Where("is_jump = ?", listReq.IsJump)
	}

	if listReq.SourceId != "" {
		model = model.Where("source_id = ?", listReq.SourceId)
	}
	if listReq.AccountId != "" {
		model = model.Where("account_id = ?", listReq.AccountId)
	}

	if listReq.PackageName != "" {
		model = model.Where("package_name = ?", listReq.PackageName)
	}
	if listReq.CityInfo != "" {
		model = model.Where("city_info like ?", "%"+listReq.CityInfo+"%")
	}

	if !listReq.StartTime.IsZero() {
		model = model.Where("create_time >= ?", listReq.StartTime.Unix())
	}
	if !listReq.EndTime.IsZero() {
		model = model.Where("create_time <= ?", listReq.EndTime.Unix()+(3600*24-1))
	}

	model = model.Where("is_delete = ?", 0)
	// 总数
	var count int64
	err := model.Count(&count).Error
	if e = response.CheckErr(err, "List Count err"); e != nil {
		return
	}
	// 数据
	var objs []client.ClientUser
	err = model.Limit(limit).Offset(offset).Order("id desc").Find(&objs).Error
	if e = response.CheckErr(err, "List Find err"); e != nil {
		return
	}
	resps := []resp.ClientUserResp{}
	response.Copy(&resps, objs)
	return response.PageResp{
		PageNo:   page.PageNo,
		PageSize: page.PageSize,
		Count:    count,
		Lists:    resps,
	}, nil
}

// Detail 用户信息详情
func (srv clientUserService) Detail(id uint) (res resp.ClientUserResp, e error) {
	var obj client.ClientUser
	err := srv.db.Where("id = ? AND is_delete = ?", id, 0).Limit(1).First(&obj).Error
	if e = response.CheckErrDBNotRecord(err, "数据不存在!"); e != nil {
		return
	}
	if e = response.CheckErr(err, "Detail First err"); e != nil {
		return
	}
	response.Copy(&res, obj)
	return
}

// Del 用户信息删除
func (srv clientUserService) Del(id uint) (e error) {
	var obj client.ClientUser
	err := srv.db.Where("id = ?", id).Limit(1).First(&obj).Error
	// 校验
	if e = response.CheckErrDBNotRecord(err, "数据不存在!"); e != nil {
		return
	}
	if e = response.CheckErr(err, "black First err"); e != nil {
		return
	}
	// 删除
	err = srv.db.Model(client.ClientUser{}).Where("id = ?", obj.ID).Select("is_black").Updates(&client.ClientUser{
		IsBlack: 1,
	}).Error

	e = response.CheckErr(err, "black Delete err")

	util.RedisUtil.HDel(config.ClientConfig.BackstageManageKey, strconv.FormatUint(uint64(obj.ID), 10))
	return
}
